#!/bin/bash
##################################################################################################
##  Purpose:Operating System Environment Configuration 
##  Author :syd
##  Created:2024-06-24
##  Version:1
##################################################################################################
# 1.日志输出格式
log(){
	LOGFILE=os_set_$(date +"%Y%m%d").log
	LOGTYPE=$1
	NOW_TIME='['$(date +"%Y-%m-%d %H:%M:%S")']'
	case $LOGTYPE in
	ERROR)
	    echo -e "\033[31m$NOW_TIME [$1]$2\033[0m" 2>&1 | tee -a $LOGFILE;;
	INFO)
	    echo -e "\033[34m$NOW_TIME [$1]$2\033[0m" 2>&1 | tee -a $LOGFILE;;
	INPUT)
	    echo -e "\033[33m$NOW_TIME [$1]$2\033[0m" 2>&1 | tee -a $LOGFILE;;
	CMD)
		echo -e -n "\033[37m$NOW_TIME $2\n\033[0m" 2>&1 | tee -a $LOGFILE
		echo -e -n "\033[37m `$2`\n\033[0m" 2>&1 | tee -a $LOGFILE;;
	esac
}

# 2.必配项
must_set(){
	systemctl stop avahi-daemon
	systemctl disable avahi-daemon
	log INFO "avahi-daemon 禁用成功！"
	
	sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
	setenforce 0
	cat /etc/selinux/config | grep SELINUX=disabled
	getenforce
	log INFO "selinux 禁用成功！"
	
	firewall-cmd --permanent --add-port=5866/tcp
	firewall-cmd --add-port=5866/tcp
	if [ $? -eq 0 ]
	then
		log INFO "5866 端口添加成功！" 
	else
		log INFO "防火墙已停止运行。"
	fi

if [ `grep HGDB /etc/security/limits.conf|wc -l` -eq 0 ]
then	
cat >> /etc/security/limits.conf <<EOF
#For HGDB
*               soft    stack     unlimited
*               hard    stack     unlimited
*               soft    nproc     unlimited
*               hard    nproc     unlimited
*               soft    nofile    1048576
*               hard    nofile    1048576
*               soft    memlock   unlimited
*               hard    memlock   unlimited
#End for HGDB
EOF

echo "session    required     pam_limits.so" >> /etc/pam.d/login
else
:
fi
log CMD "ulimit -a"

if [ `grep HISTFILESIZE /etc/profile|wc -l` -eq 0 ]
then
cat >> /etc/profile <<EOF
export HISTSIZE=2000
export HISTFILESIZE=50000
USER_IP=\`who -u am i 2>/dev/null| awk '{print \$NF}'|sed -e 's/[()]//g'\`
export HISTTIMEFORMAT="[%F %T][\`whoami\`][\${USER_IP}] "
export PROMPT_COMMAND="history -a"
EOF

source  /etc/profile
else
:
fi
}

# 3.选配项
choice_set(){
	while [[ ! $t =~ [y,Y,n,N] ]]
	do
	read -p "是否需要关闭防火墙y/n【默认n】：" t
	log INFO "是否需要关闭防火墙y/n【默认n】，你选择${t:=n}。"
	case $t in
		Y|y)
			systemctl stop firewalld.service
			systemctl disable firewalld.service
			;;
		N|n)
			:
			;;
		*)
			log ERROR "只可输入n或y，请重新输入！"
			exit;
	esac
	done
	
	while [[ ! $m =~ [y,Y,n,N] ]]
	do
	read -p "是否需要关闭 NetworkManagery/n【默认n】：" m
	log INFO "是否需要关闭 NetworkManagery/n【默认n】，你选择${m:=n}。"
	case $m in
		Y|y)
			systemctl stop NetworkManager.service
			systemctl disable NetworkManager.service
			;;
		N|n)
			:
			;;
		*)
			log ERROR "只可输入n或y，请重新输入！"
			exit;
	esac
	done
	
log INPUT "*****************************************************************************************************************" 
log INPUT "************************************系统环境配置完成！请手动检查确认如下信息*************************************" 
log INPUT "*****************************************************************************************************************" 
log INPUT "1.确保安装包与操作系统环境匹配"
log INPUT "cat /etc/*release，lscpu，nkvers，rpm --version，dpkg --version，md5sum hgdb4.5.8-see-xxxxxxxxxxx"
log INPUT "2.主机名检查"
log INPUT "3.网络检查"
log INPUT "ip addr，ethtool 网卡名，scp local_file remote_username@remote_ip:remote_folder（结果小于50mb/s具有一定风险）"
log INPUT "4.磁盘检查"
log INPUT "df -h (确保有挂载数据盘)"
log INPUT "dd if=/dev/zero of=/data/testio bs=32k count=20k oflag=dsync(测试写入小文件，结果低于20MB/s，则说明磁盘性能偏差)"
log INPUT "dd if=/dev/zero of=/data/testio bs=8192k count=100 oflag=dsync(测试写入大文件，结果低于200MB/s，则说明磁盘性能偏差)"
log INPUT "5.cpu 内存检查(生产环境建议最少8核32G)"
log INPUT "lscpu，free -h"
}


# 4.主函数
main(){
	log
	must_set
	choice_set
} 

# 5.执行
main